package com.sgb.kjwl.view.ui.activity.self;

import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.viewpager.widget.ViewPager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPhotoViewBinding;
import com.sgb.kjwl.model.entity.self.VideoEntity;
import com.sgb.kjwl.view.ui.adapter.self.VideoPagerAdapter;

import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

public class VideoViewActivity extends AppActivity {
    public static final String KEY_VIDEO_URLS = "video";
    public static final String KEY_VIDEO_POSITION = "position";
    ActivityPhotoViewBinding binding;
    //视频
    private SensorManager sensorManager;
    private SensorEventListener sensorEventListener;
    private int size;

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void init() {
        List<VideoEntity> videoEntityList = (List<VideoEntity>) getIntent().getSerializableExtra(VideoViewActivity.KEY_VIDEO_URLS);
        size = videoEntityList.size();
        int position = getIntent().getIntExtra(KEY_VIDEO_POSITION, 0);
        binding.pageView.setText((position + 1) + "/" + size);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorEventListener = new JCVideoPlayer.JCAutoFullscreenListener();
        VideoPagerAdapter videoPagerAdapter = new VideoPagerAdapter(VideoViewActivity.this, videoEntityList);
        binding.viewpager.setAdapter(videoPagerAdapter);
        binding.viewpager.setCurrentItem(position);
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                binding.pageView.setText((position + 1) + "/" + size);
                //继续播放
                videoPagerAdapter.startVideo(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(VideoViewActivity.this, R.layout.activity_photo_view);
        boolean isGoneBack = getIntent().getBooleanExtra("goneBack", false);
        if (!isGoneBack) {
            binding.tvActionBarIntroduction.setVisibility(View.VISIBLE);
        } else {
            binding.tvActionBarIntroduction.setVisibility(View.GONE);
        }
        binding.actionBarImageLeft.setOnClickListener(this);
    }

    @Override
    public void onMultiClick(View v) {
        switch (v.getId()) {
            case R.id.action_bar_image_left:
                finish();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(sensorEventListener);
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }
}
